package cn.tedu.sync.reentrant;

public class Demo {

    // synchronized
    public static void main(String[] args) {
        Demo demo = new Demo();

        Thread t = new Thread(demo::sayC); // this
        Thread t1 = new Thread(demo::sayC);// 局部变量
        t.start();
        t1.start();
    }

    public synchronized void say() {
        System.out.println("进入say");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("说话");
    }

    public void sayA() {
        System.out.println("进入sayA");
        synchronized (this) {
            System.out.println("你好sayA");
        }
    }

    public void sayB() {
        synchronized ("lock") {
            System.out.println("你好");
        }
    }

    public void sayClass() {

        synchronized (Demo.class) {
            System.out.println("你好我是C");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void sayC() {
        // -128-127  JVM 经常会用
        Integer i = 127;
        synchronized (i) {
            System.out.println("你好我是C");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
